<?php

class Test_Storage_AwsS3 {
    
    private $_storage = null;
    
    public function __construct() {
        
        V::addLibrary('third', dirname(V_PATH).'/third-lib');

        V::loadConfig(array(
            'aws_client' => array(
                'profile' => 'default',
                'credentials_filename' => dirname(__FILE__).'/aws.credentials',
                'region'          => 'ap-guangzhou',
                'version'         => '2006-03-01',
                'endpoint'        => 'https://cos.ap-guangzhou.myqcloud.com'
            ),
        ));
        $region = 'ap-guangzhou';
        $bucket = 'public-tmp-1252772859';
        $url = "http://{$bucket}.cos.{$region}.myqcloud.com";
        $this->_storage = Storage::factory('AwsS3', array(
            'base_url' => $url,
            'bucket' => $bucket,
        ));
    }

    /**
     * 测试读写
     */
    public function testWriteRead() {
        
        $path = 'test/'.time().'.txt';
        $rawContent = 'this is a test #'.time();
        $this->_storage->write($path, $rawContent);
        echo 'url: ', PHP_EOL, $this->_storage->getUrl($path), PHP_EOL;
        $content = $this->_storage->read($path);
        echo 'content: ', PHP_EOL, $content, PHP_EOL;
        $this->_storage->remove($path);
        return $rawContent == $content ? true : false;
    }
}